/**
 * SharedIdea.java
 * Purpose: Model
 *
 * @version 1.0 5/14/11
 * @author Islam Al-Aarag
 */
package models;

import play.*;
import play.db.jpa.*;

import javax.persistence.*;
import java.util.*;

@Entity
public class SharedPlan extends Model {
	@Lob
	public String message;
	public Date postedAt;
	public String history;
	@ManyToOne
	public User sharer;
	@ManyToOne
	public User sharedWith;
	@ManyToOne
	public Plan sharedPlan;

	public SharedPlan(String message, User sharer, User sharedWith,
			Plan sharedPlan) {
		if (message.equals("null"))
			message = "";
		this.message = message;
		history = "";
		this.sharer = sharer;
		this.sharedWith = sharedWith;
		this.sharedPlan = sharedPlan;
		postedAt = new Date();
		this.save();
		sharedWith.sharedPlans.add(this);
		sharedWith.save();
	}
	/**
	 * @author Ahmed Mahmoud
	 * @return
	 * 
	 */
	public boolean cascadeDelete(){
		sharer.sharedIdeas.remove(this);
		sharer.save();
		sharedWith.sharedIdeas.remove(this);
		sharedWith.save();

		sharedPlan.sharedPlans.remove(this);

		sharedPlan.save();

		this.delete();
		return true;
	}

}
